from django.http import HttpResponse
def index_view(request):
    html = '<h1>这是首页</h1>'
    return HttpResponse(html)
def test_get(request):
    # request 请求的对象
    print('请求的方法：', request.method) # GET
    print('请求的路径：', request.path_info)  # /test_get
    # <QueryDict: {'a': ['1'], 'b': ['2']}>
    print('GET请求的参数：', request.GET)
    print('a:', request.GET['a'])
    # print('c:', request.GET['c']) # 报错
    print('c:', request.GET.get('c', 'ok')) # 不存在返回None，可以设置默认值
    print('获取多个a:', request.GET.getlist('a')) # ['1', '3']

    return HttpResponse('GET请求测试')

html = '''
    <h2>用户登录</h2>
    <form action="/test_get_post" method="post">
        用户：<input type="text" name="username"><br>
        密码：<input type="password" name="password"><br>
        <input type="submit">
    </form>
'''
def test_get_post(request):
    # 判断请求方法是哪一种
    if request.method == 'GET':
        return HttpResponse(html)
    elif request.method == 'POST':
        # 获取POST请求的参数
        # <QueryDict: {'username': ['小明'], 'password': ['123456']}>
        print('POST请求参数：', request.POST)
        print('用户名：', request.POST['username'])
        print('密码：', request.POST.get('password'))
        # getlist()

        return HttpResponse('这是一个POST请求')

html = '''
    <h2>用户注册</h2>
    <form action="/register" method="post">
        用户：<input type="text" name="username"><br>
        密码：<input type="password" name="password"><br>
        兴趣：<input type="checkbox" name="hobby" value="看书">看书
            <input type="checkbox" name="hobby" value="旅游">旅游
            <input type="checkbox" name="hobby" value="音乐">音乐
            <br>
        性别：<input type="radio" name="gender" value="男">男
             <input type="radio" name="gender" value="女">女
             <br>
        <input type="submit">
    </form>
'''
def register(request):
    if request.method == 'GET':
        return HttpResponse(html)
    elif request.method == 'POST':
        # 获取传递的参数
        info = request.POST
        print(info)
        return HttpResponse(f'''
            注册成功!欢迎{info["username"]}
            <br>
            密码：{info["password"]}
            <br>
            兴趣：{'/'.join(info.getlist("hobby"))}
            <br>
            性别：{info["gender"]}
            
        ''')
# 导入渲染函数（用于将数据传递模板）
from django.shortcuts import render
def test_html(request):
    name = '小明'
    age = 20
    # 响应模板文件，自动到templates搜索文件
    # 把值提供给模板，必须是字典格式
    return render(request, 'test_html.html', {'name': name, 'age': age})

def test_html_param(request):
    mydict = {
        'title': '小米15',
        'price': 4999,
        'color': ['红色', '蓝色', '绿色'],
        'book': {'a': 1, 'b': 2}
    }
    return render(request, 'test_html_param.html', mydict)

def test_if_for(request):
    mydict = {
        'score': 52,
        'n': 3,
        'city': ['北京', '长沙', '成都', '西安']  #
    }
    return render(request, 'test_if_for.html', mydict)
def calc(request):
    # 判断请求方法
    if request.method == 'GET':
        return render(request, 'calc.html')
    elif request.method == 'POST':
        # 获取POST传递的参数
        info = request.POST
        # <QueryDict: {'num1': ['1'], 'op': ['+'], 'num2': ['2']}>
        # print(info)
        op = info['op']
        num1 = int(info['num1'])
        num2 = int(info['num2'])
        if op == '+':
            result = num1 + num2
        elif op == '-':
            result = num1 - num2
        elif op == '*':
            result = num1 * num2
        else:
            result = num1 / num2
        # locals()  可以自动将当前函数中的变量放入到字典
        # 将结果返回给模板
        return render(request, 'calc.html', locals())



